package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.SysUserDeptBo;
import com.ruoyi.zhgd.domain.vo.SysUserDeptVo;
import com.ruoyi.zhgd.domain.SysUserDept;
import com.ruoyi.zhgd.mapper.SysUserDeptMapper;
import com.ruoyi.zhgd.service.ISysUserDeptService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户隶属机构Service业务层处理
 *
 * @author zyb
 * @date 2023-09-13
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class SysUserDeptServiceImpl implements ISysUserDeptService {

    private final SysUserDeptMapper baseMapper;

    /**
     * 查询用户隶属机构
     */
    @Override
    public SysUserDeptVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户隶属机构列表
     */
    @Override
    public TableDataInfo<SysUserDeptVo> queryPageList(SysUserDeptBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysUserDept> lqw = buildQueryWrapper(bo);
        Page<SysUserDeptVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户隶属机构列表
     */
    @Override
    public List<SysUserDeptVo> queryList(SysUserDeptBo bo) {
        LambdaQueryWrapper<SysUserDept> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysUserDept> buildQueryWrapper(SysUserDeptBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysUserDept> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, SysUserDept::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getDeptPkid()), SysUserDept::getDeptPkid, bo.getDeptPkid());
        lqw.eq(StringUtils.isNotBlank(bo.getDeptId()), SysUserDept::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getCompanyId()), SysUserDept::getCompanyId, bo.getCompanyId());
        lqw.eq(StringUtils.isNotBlank(bo.getCompanyPkid()), SysUserDept::getCompanyPkid, bo.getCompanyPkid());
        lqw.eq(bo.getHasMain() != null, SysUserDept::getHasMain, bo.getHasMain());
        lqw.eq(bo.getOrderId() != null, SysUserDept::getOrderId, bo.getOrderId());
        lqw.eq(bo.getCreator() != null, SysUserDept::getCreator, bo.getCreator());
        lqw.eq(bo.getCreateDate() != null, SysUserDept::getCreateDate, bo.getCreateDate());
        lqw.eq(bo.getUpdater() != null, SysUserDept::getUpdater, bo.getUpdater());
        lqw.eq(bo.getUpdateDate() != null, SysUserDept::getUpdateDate, bo.getUpdateDate());
        return lqw;
    }

    /**
     * 新增用户隶属机构
     */
    @Override
    public Boolean insertByBo(SysUserDeptBo bo) {
        SysUserDept add = BeanUtil.toBean(bo, SysUserDept.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户隶属机构
     */
    @Override
    public Boolean updateByBo(SysUserDeptBo bo) {
        SysUserDept update = BeanUtil.toBean(bo, SysUserDept.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysUserDept entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户隶属机构
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
